//
//  LSUserFeedbackVController.m
//  LookScore
//
//  Created by wr on 2017/12/8.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSUserFeedbackVController.h"
#import "ACSelectMediaView.h"
#import "UITextView+Placeholder.h"
@interface LSUserFeedbackVController ()<UITextViewDelegate>
//@property (weak, nonatomic) IBOutlet UITextView *feedbackTV;
//@property (weak, nonatomic) IBOutlet UITextField *connectTF;
//@property (weak, nonatomic) IBOutlet UIButton *sureBu;



@end

@implementation LSUserFeedbackVController{
    NSMutableArray *imgArray;
    UILabel * _bottomLabel;
    UITextView          *_inputTextView;//文本输入框
    NSString *contentStr;
    NSString *contactStr;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [[IQKeyboardManager sharedManager] setEnable:NO];
//    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    self.navigationController.navigationBar.translucent = NO;

//
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"用户反馈";
    self.view.backgroundColor = LSBackGroundColor;
    contactStr = @"";
  
    //容器view
    UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, LSAutoVlaue(300, LSScreenWidth))];
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
    
    //文本输入框
    NSInteger leftPadding=8;
    UITextView *inputTextView=[[UITextView alloc]initWithFrame:CGRectMake(leftPadding, leftPadding,LSScreenWidth-leftPadding*2,50)];
    inputTextView.showsVerticalScrollIndicator=YES;
    inputTextView.returnKeyType   = UIReturnKeyDefault;
    inputTextView.backgroundColor   =   [UIColor whiteColor];
    inputTextView.placeholder=@"请描述一下您的问题...";
    [inputTextView setTextColor:[UIColor blackColor]];
    inputTextView.placeholderColor=Color(148, 148, 148);
    inputTextView.delegate=self;
    _inputTextView=inputTextView;
    [inputTextView setFont:[UIFont systemFontOfSize:15]];
    [bgView addSubview:inputTextView];
    
    
    ACSelectMediaView * mediaView = [[ACSelectMediaView alloc] initWithFrame:CGRectMake(25, inputTextView.maxY, LSScreenWidth-50, 1)];
    mediaView.type = ACMediaTypePhotoAndCamera;
    mediaView.maxImageSelected = 3;
    mediaView.naviBarBgColor = LSNavMainColor;
    mediaView.rowImageCount =  3;
    mediaView.lineSpacing = 5;
    mediaView.interitemSpacing = 5;
    mediaView.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    [mediaView observeViewHeight:^(CGFloat mediaHeight) {

    }];

    [bgView addSubview:mediaView];
    bgView.height = mediaView.maxY+20;
    [mediaView observeSelectedMediaArray:^(NSArray<ACMediaModel *> *list) {
        imgArray = @[].mutableCopy;
        for (ACMediaModel * model in list) {
            [imgArray addObject:model.image];
        }
        bgView.frame = CGRectMake(0, 0, LSScreenWidth, mediaView.maxY + 20);

    }];
    
    UILabel * bottomLabel = [[UILabel alloc] init];
    bottomLabel.textAlignment = NSTextAlignmentRight;
    bottomLabel.textColor=LSTitleGrayColor2;
    bottomLabel.font = LSFontSize12;
    bottomLabel.text = @"0/1000";
    [bgView addSubview:bottomLabel];
    
    _bottomLabel = bottomLabel;
    bottomLabel.sd_layout
    .rightSpaceToView(bgView, 10)
    .bottomSpaceToView(bgView, 0)
    .heightIs(20);
    [bottomLabel setSingleLineAutoResizeWithMaxWidth:180];
//    MyLog(@"%f",mediaView.maxY);
    
    UILabel *lable = [[UILabel alloc] init];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = @"联系方式（选填）";
    lable.font = LSFontSize14;
    [self.view addSubview:lable];
    lable.sd_layout.leftEqualToView(self.view).offset(20).topSpaceToView(bgView, 20).widthIs(115).heightIs(30);
    
    UITextField *contactTextField = [[UITextField alloc] init];
    contactTextField.clearButtonMode=UITextFieldViewModeAlways;
    contactTextField.placeholder = @"电话/微信/QQ";
    [contactTextField addTarget:self action:@selector(getTextString:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:contactTextField];
    contactTextField.sd_layout.leftSpaceToView(lable, 10).centerYEqualToView(lable).rightEqualToView(self.view).offset(-10).heightIs(lable.height);
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = LSLineGrayColor;
    [self.view addSubview:line];
    line.sd_layout.leftSpaceToView(lable, 0).bottomEqualToView(lable).rightEqualToView(self.view).offset(-10).heightIs(0.5);
    
    
    UIButton *sureBu = [[UIButton alloc] init];
    [sureBu addTarget:self action:@selector(clickSureButton:) forControlEvents:UIControlEventTouchUpInside];
    [sureBu setTitle:@"确定" forState:UIControlStateNormal];
    [sureBu setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    sureBu.titleLabel.font = LSFontSize15;
    [sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateNormal];
    [sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forState:UIControlStateHighlighted];
    sureBu.layer.masksToBounds = YES;
    sureBu.layer.cornerRadius = 4;
    [self.view addSubview:sureBu];
    sureBu.sd_layout.leftEqualToView(self.view).offset(50).rightEqualToView(self.view).offset(-50).topSpaceToView(lable, 30).heightIs(50);
    
    
}
#pragma mark 🔥 UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    
    contentStr=textView.text;
    _bottomLabel.text = [NSString stringWithFormat:@"%zd/%d",contentStr.length,1000];
    //    _issueBottomView.wordsNumber=inputText.length;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    NSString *inputString=[textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if (inputString.length>1000) {
        
        NSString *msg=[NSString stringWithFormat:@"字数不能超过%d",1000];
        [MBProgressHUD showOnlyMessage:msg afterDelay:1.5f];
        
        return NO;
    }else{
        return YES;
    }
}
#pragma mark 🔥 UITextViewDelegate

//#pragma mark 🔥 UITextFieldDelegate
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    contactStr = textField.text;
//    MyLog(@"contactStr:%@",contactStr);
//    return YES;
//}

- (void)getTextString:(UITextField *)textField{
        contactStr = textField.text;
    MyLog(@"contactStr ----- %@",contactStr);
}

#pragma mark 🔥 事件

- (void)clickSureButton:(UIButton *)sender{
    if (contentStr.length > 0) {
        if (imgArray.count != 0) {
            [LSHttpEnclosureTool Upload:LSUSERUPLOADIMG parameters:@{@"type":@"2"} uploadParamArray:imgArray resBlock:^(id responseObject) {
                if (responseObject) {
                    
                    [self feedBackWithImgStr:responseObject];
                    MyLog(@"1111%@",responseObject);
                }
            }];
        }else{
            
            [self feedBackWithImgStr:@""];
        }
        
    }
    
    else{
        
        [MBProgressHUD showOnlyMessage:@"请描述一下您遇到的问题" afterDelay:1.5];
        
    }
}
- (void)feedBackWithImgStr:(NSString *)imgStr{
    NSDictionary * dict = @{@"userCid":[LSAccountArchive account].Cid,@"content":contentStr,@"imgUrlStr":imgStr,@"contactWay":contactStr};
    MyLog(@"%@",dict);
    [LSHttpEnclosureTool POST:LSUSERFEEDBACK parameters:dict resBlock:^(id responseObject) {
        if (responseObject) {
            [MBProgressHUD showOnlyMessage:@"反馈成功" afterDelay:1.5];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
